﻿namespace EasyGo.Common.Auth
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class UserRole
    {
        public static readonly UserRole Admin = new UserRole("admin", "Admin");

        public static readonly UserRole Worker = new UserRole("worker", "Worker");

        public static readonly UserRole Customer = new UserRole("customer", "Customer");

        private UserRole(string key, string description)
        {
            this.Key = key;
            this.Description = description;
        }

        public static IEnumerable<UserRole> All
        {
            get
            {
                yield return Admin;
                yield return Worker;
                yield return Customer;
            }
        }

        public string Key { get; private set; }

        public string Description { get; private set; }

        public static UserRole Create(string str)
        {
            var role = All.SingleOrDefault(x => x.Key == str);

            if (role == null)
            {
                throw new ArgumentException("User role: " + str + " not exists.");
            }

            return role;
        }

        public static implicit operator string(UserRole item)
        {
            return item.Key;
        }

        public override string ToString()
        {
            return this.Key;
        }
    }
}
